Eureka搭建

发布于:2025-04-21 ⋅ 阅读:(10) ⋅ 点赞:(0)
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();
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到