Eureka搭建

1.注册中心server端
1.1.引入依赖
复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.2.配置
复制代码
server:
  port: 8761
spring:
  application:
    name: eureka-server #服务名称,如多台server,那么保持一致
  security: #security用户名密码
    user:
      name: root
      password: root
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true #设置显示ip地址
    instance-id: ${spring.cloud.client.ipaddress}:${server.port}
  client:
    fetch-registry: false #是否注册自己默认为true,如果
    register-with-eureka: false
    service-url: #注册中心地址  root:root@ 为security用户名密码
      defaultZone: http://root:root@localhost:8761/eureka/ #http://root:root@${eureka.instance.hostname}:${server.port}/eureka/
1.3.server端security配置
复制代码
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter  {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        //解决/eureka下的跨域问题
        http.csrf().ignoringAntMatchers("/eureka/**");
    }

}
1.4.启动类加上
复制代码
@EnableEurekaServer
2.provider服务提供方
2.1.client端依赖
复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.2.client配置
复制代码
server:
  port: 7070

spring:
  application:
    name: service-provider #服务名称,多来provider服务名保持一致

eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipaddress}:${server.port}
  client:
    service-url:	
      defaultZone: http://root:root@localhost:8761/eureka/ #,http://localhost:8762/eureka/ 注册中心url多个注册中心用","分隔

service-provider: #ribbon配置 service-provider为服务名
  ribbon: #RandomRule为轮询
    NFLoadBlanceRuleClassName: com.netflix.loadblancer.RandomRule
2.3.提供服务接口
复制代码
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @GetMapping("/getUserByName/{name}")
    public User getUserByName(@PathVariable String name){
        log.info("name={}",name);
        return new User("王渝",22);
    }
}
2.4.开启client
复制代码
@EnableEurekaClient //可以不加
3.consumer服务使用方
3.1.依赖
复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3.2.配置
复制代码
server:
  port: 9090

spring:
  application:
    name: service-consumer

eureka:
  client:
    service-url: #注册中心向外暴露的地址
      defaultZone: http://root:root@localhost:8761/eureka/ #,http://localhost:8762/eureka/  多台server已逗号分隔
    register-with-eureka: false
    registry-fetch-interval-seconds: 10 
    #表示EurekaClient间隔多久去拉去注册信息 默认30秒
3.3.Controller接口
复制代码
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceFeign userServiceFeign;

    @GetMapping("/name2/{name}")
    public User getUserByName2(@PathVariable String name){
        return  userServiceFeign.getUserName(name);
    }
}
3.4.Service接口(方法一 feign调用)
复制代码
//service-provide为服务提供方的名称
@FeignClient(value = "service-provider") 
public interface UserServiceFeign {
    
	// /user/getUserByName/{name} 为消息提供方的url
    @GetMapping("/user/getUserByName/{name}") 
    User getUserName(@PathVariable(value = "name") String name);


}
3.5Service接口(方法二 RestTemplate)
复制代码
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public User getUserByName(String name) {
        ResponseEntity<User> response = restTemplate.exchange("http://service-provider/user/getUserByName/" + name,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<User>() {
                });

        return response.getBody();
    }
}
相关推荐
RainbowSea6 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea6 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑10 小时前
Jpa使用union all
java·spring boot·后端
用户37215742613510 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊11 小时前
Java学习第22天 - 云原生与容器化
java
渣哥13 小时前
原来 Java 里线程安全集合有这么多种
java
间彧13 小时前
Spring Boot集成Spring Security完整指南
java
间彧14 小时前
Spring Secutiy基本原理及工作流程
java
Java水解15 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆17 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试