srpingcolud简单的小工程
来源:互联网 发布:vb.net sleep 编辑:程序博客网 时间:2024/05/22 04:26
Ø 微服务架构搭建步骤1——创建服务注册中心
Ø 微服务架构搭建步骤2——创建服务提供方
Ø 微服务架构搭建步骤3——创建服务消费方
注册中心springcolud_test
Pom文件不需加其他配置
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.idcard.interview</groupId> <artifactId>springcolud_test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcolud_test</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--spring cloud center pom--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <!--spring cloud center management plugin--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
注册中心配置
#服务注册中心
spring.application.name=springboot_test
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
在入口程序中添加注解
@SpringBootApplication
@EnableEurekaServer
public class SpringcoludTestApplication{
public static void main(String[]args) {
SpringApplication.run(SpringcoludTestApplication.class, args);
}
}
创建服务提供方springboot_service
可以加其他pom,但是相应配置必须加入,如不加入会出现错误
<!--服务提供方的依赖包-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置
#spring cloud配置
spring.application.name=springboot_service
server.port=2222
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
在入口程序中添加注解
@SpringBootApplication
@EnableDiscoveryClient
public class SpringbootServiceApplication{
public static void main(String[]args) {
SpringApplication.run(SpringbootServiceApplication.class, args);
}
}
springboot_service提供方的controller
@RestController public class ComputerController { @RequestMapping("/plus/{a}/{b}") public Integer plus(@PathVariable Integer a, @PathVariable Integer b){ return a+b; }
springboot _ consumer消费方
注入依赖
<dependencies>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies><dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>
配置
sspring.application.name=springboot_consumerserver.port=3333eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
在入口程序中添加注解并注入RestTemplate到bean容器
@SpringBootApplication
@EnableDiscoveryClient
public class SpringbootConsumerApplication{
@Bean
public RestTemplaterestTemplate() {
return new RestTemplate();
}
public static void main(String[]args) {
SpringApplication.run(SpringbootConsumerApplication.class, args);
}
在springboot_consumer controller中注入
@Autowiredprivate LoadBalancerClient lbc;@Autowiredprivate RestTemplate template;@RequestMapping("/comsumer/{a}/{b}")public String computePlus(@PathVariable Integer a, @PathVariable Integer b){ //发现服务 ServiceInstance choose = lbc.choose("springboot_service"); String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/plus/"+a+"/"+b; Integer result = template.getForObject(url, Integer.class); return a+"+"+b+"="+result;}
访问路径
http://localhost:3333/comsumer/1/2s
- srpingcolud简单的小工程
- 简单的ReactNative工程
- opencv常用的小工程
- redis的小测试工程
- 简单工程Makefile的编写
- 构建简单的SPRING工程
- 简单工程Makefile的编写
- 一个简单的SpringMVC工程
- 简单工程Makefile的编写
- 编译简单的Vuforia工程
- 简单的裸机makefile工程
- 简单工程Makefile的编写
- 简单autoconf 工程的编写
- 简单的SpringBoot工程搭建
- 简单的小计算器
- 简单的小计算器
- 简单的小动画
- 简单的小幸福
- 剑指offer(一)二维数组中的查找
- Chrome 扩展程序——Imagus:图片放大预览工具
- Effective Java 创建和销毁对象 1.考虑用静态工厂方法代替构造器
- Vue列表过渡
- 算法训练 数组查找及替换
- srpingcolud简单的小工程
- ccsv6添加使用csl6455lib
- git操作之ssh与https互换
- 用innerHTML插入html代码中有样式表时需注意的地方
- hibernate配置
- HDU 6162 树链剖分
- 【转】和Oracle数据库的参数有关的几个试图
- “星星评级”样式表单设计
- Java中的匿名内部类