当前位置: 首页 > backend >正文

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: rootpassword: root
eureka:instance:hostname: localhostprefer-ip-address: true #设置显示ip地址instance-id: ${spring.cloud.client.ipaddress}:${server.port}client:fetch-registry: false #是否注册自己默认为true,如果register-with-eureka: falseservice-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  {@Overrideprotected 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: 7070spring:application:name: service-provider #服务名称,多来provider服务名保持一致eureka:instance:hostname: localhostprefer-ip-address: trueinstance-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: 9090spring:application:name: service-consumereureka:client:service-url: #注册中心向外暴露的地址defaultZone: http://root:root@localhost:8761/eureka/ #,http://localhost:8762/eureka/  多台server已逗号分隔register-with-eureka: falseregistry-fetch-interval-seconds: 10 #表示EurekaClient间隔多久去拉去注册信息 默认30秒
3.3.Controller接口
@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate 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 {@Autowiredprivate RestTemplate restTemplate;@Overridepublic User getUserByName(String name) {ResponseEntity<User> response = restTemplate.exchange("http://service-provider/user/getUserByName/" + name,HttpMethod.GET,null,new ParameterizedTypeReference<User>() {});return response.getBody();}
}
http://www.xdnf.cn/news/694.html

相关文章:

  • BeautifulSoup 库的使用——python爬虫
  • 算法—合并排序—js(场景:大数据且需稳定性)
  • 23种设计模式-结构型模式之装饰器模式(Java版本)
  • C#进阶学习(八)常见的泛型数据结构类(3)SortedDictionary<TKey, TValue>与SortedList<TKey, TValue>
  • 大语言模型推理能力的强化学习现状理解GRPO与近期推理模型研究的新见解
  • PG CTE 递归 SQL 翻译为 达梦版本
  • 将 JSON 字符串转化为对象的详细笔记 (Java示例)
  • 【AI量化第26篇】以配置为核心的工程化研究管理——基于miniQMT的量化交易回测系统开发实记
  • 15. 三数之和
  • 计算机网络中的网络层:架构、功能与重要性
  • llama factory
  • springboot+vue3+mysql+websocket实现的即时通讯软件
  • C++数组栈与链表栈
  • 软考高级系统架构设计师-第16章 数学与经济管理
  • 切换 Python 版本(配置path方式,含trae)
  • 一个最简单的 Model Context Protocol 的例子
  • Halcon应用:相机标定
  • C++入门篇(下)
  • 线性DP:最长上升子序列(可不连续,数组必须连续)
  • Matlab 复合模糊PID
  • NumPy:数值计算基础与高性能数组操作
  • 如何使用人工智能大模型,免费快速写工作总结?
  • Linux基础指令 补充(自用)
  • 【微知】服务器如何获取服务器的SN序列号信息?(dmidecode -t 1)
  • Origin将双Y轴柱状图升级为双向分组柱状图
  • 二、在springboot 中使用 AIService
  • 【JAVA EE初阶】多线程(1)
  • 代码随想录算法训练营第五十三天 | 105.有向图的完全可达性 106.岛屿的周长
  • 如何轻松实现用户充值系统的API自动化测试
  • QML、Qt Quick 、Qt Quick Controls 2