(一)搭建注册与配置中心CSE
从官网下载对应版本下载地址 解压文件,然后执行 cse.exe 访问 http://127.0.0.1:30103 则可看到 CSE 页面
(二)项目总pom文件
1. pom.xml 文件配置
< ? 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. coffee< / groupId>
< artifactId> chassis- demo< / artifactId>
< packaging> pom< / packaging>
< version> 1.0 - SNAPSHOT< / version>
< modules>
< module > chassis- provider< / module >
< module > chassis- consumer< / module >
< module > edge< / module >
< / modules>
< properties>
< servicecomb. version> 2.7 .5 < / servicecomb. version>
< / properties>
< dependencyManagement>
< dependencies>
< dependency>
< groupId> org. apache. servicecomb< / groupId>
< artifactId> java- chassis- dependencies< / artifactId>
< version> ${ servicecomb. version} < / version>
< type> pom< / type>
< scope> import < / scope>
< / dependency>
< / dependencies>
< / dependencyManagement>
< dependencies>
< dependency>
< groupId> org. apache. servicecomb< / groupId>
< artifactId> solution- basic< / artifactId>
< / dependency>
< dependency>
< groupId> org. apache. servicecomb< / groupId>
< artifactId> registry- service- center< / artifactId>
< / dependency>
< dependency>
< groupId> org. apache. servicecomb< / groupId>
< artifactId> servicestage- environment< / artifactId>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org. owasp< / groupId>
< artifactId> dependency- check- maven< / artifactId>
< version> 7.0 .0 < / version>
< executions>
< execution>
< goals>
< goal> check< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>
< / plugins>
< pluginManagement>
< plugins>
< plugin>
< groupId> org. apache. maven. plugins< / groupId>
< artifactId> maven- compiler- plugin< / artifactId>
< version> 3.1 < / version>
< configuration>
< source> 1.8 < / source>
< target> 1.8 < / target>
< compilerArgument> - parameters< / compilerArgument>
< / configuration>
< / plugin>
< / plugins>
< / pluginManagement>
< / build>
< / project>
(三)服务提供者(chassis-provider)
1. pom.xml 配置
< ? 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" >
< parent>
< groupId> com. coffee< / groupId>
< artifactId> chassis- demo< / artifactId>
< version> 1.0 - SNAPSHOT< / version>
< / parent>
< modelVersion> 4.0 .0 < / modelVersion>
< artifactId> chassis- provider< / artifactId>
< dependencies>
< dependency>
< groupId> org. apache. servicecomb< / groupId>
< artifactId> java- chassis- spring- boot- starter- servlet< / artifactId>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< version> 2.1 .6 . RELEASE< / version>
< / plugin>
< / plugins>
< / build>
< / project>
2. application.yml 配置
server:
port: 5010
servicecomb:
service:
application: chassis- application
name: chassis- provider
version: 0.0 .1
registry:
address: http: / / localhost: 30100
kie:
serverUri: http: / / localhost: 30110
rest:
address: 0.0 .0 .0 : 5010
3. Controller 编写
@RestSchema ( schemaId = "ProviderController" )
@RequestMapping ( path = "/" )
public class ProviderController {
@GetMapping ( "/sayHello" )
public String sayHello ( @RequestParam ( "name" ) String name) {
return "Hello " + name + ", This is Servicecomb Java Chassis + CSE!" ;
}
}
4. 主启动类
@SpringBootApplication
@EnableServiceComb
public class ChassisProviderApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( ChassisProviderApplication . class , args) ;
}
}
(四)服务消费者(chassis-consumer)
1. pom.xml 配置
< ? 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" >
< parent>
< groupId> com. coffee< / groupId>
< artifactId> chassis- demo< / artifactId>
< version> 1.0 - SNAPSHOT< / version>
< / parent>
< modelVersion> 4.0 .0 < / modelVersion>
< artifactId> chassis- consumer< / artifactId>
< dependencies>
< dependency>
< groupId> org. apache. servicecomb< / groupId>
< artifactId> java- chassis- spring- boot- starter- servlet< / artifactId>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< version> 2.1 .6 . RELEASE< / version>
< / plugin>
< / plugins>
< / build>
< / project>
2. application.yml 配置
server:
port: 5020
servicecomb:
service:
application: chassis- application
name: chassis- consumer
version: 0.0 .1
registry:
address: http: / / localhost: 30100
kie:
serverUri: http: / / localhost: 30110
rest:
address: 0.0 .0 .0 : 5020
3. Controller 编写
@RestSchema ( schemaId = "ConsumerController" )
@RequestMapping ( path = "/" )
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping ( "/sayHello" )
public String sayHello ( @RequestParam ( "name" ) String name) {
return restTemplate. getForObject (
"servicecomb://chassis-provider/sayHello?name=" + name,
String . class ) ;
}
}
4. 主启动类
@SpringBootApplication
@EnableServiceComb
public class ChassisConsumerApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( ChassisConsumerApplication . class , args) ;
}
}
(五)网关(edge)
1. pom.xml 配置
< ? 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" >
< parent>
< groupId> com. coffee< / groupId>
< artifactId> chassis- demo< / artifactId>
< version> 1.0 - SNAPSHOT< / version>
< / parent>
< modelVersion> 4.0 .0 < / modelVersion>
< artifactId> edge< / artifactId>
< dependencies>
< dependency>
< groupId> org. apache. servicecomb< / groupId>
< artifactId> java- chassis- spring- boot- starter- standalone< / artifactId>
< / dependency>
< dependency>
< groupId> org. apache. servicecomb< / groupId>
< artifactId> edge- core< / artifactId>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- maven- plugin< / artifactId>
< version> 2.1 .6 . RELEASE< / version>
< / plugin>
< / plugins>
< / build>
< / project>
2. application.yml 配置
server:
port: 5000
servicecomb:
service:
application: chassis- application
name: edge
version: 0.0 .1
registry:
address: http: / / localhost: 30100
kie:
serverUri: http: / / localhost: 30110
rest:
address: 0.0 .0 .0 : 5000
http:
dispatcher:
edge:
default :
enabled: false
url:
enabled: true
pattern: / ( . *)
mappings:
consumer:
prefixSegmentCount: 0
path: "/.*"
microserviceName: chassis- consumer
versionRule: 0.0 .0 +
3. 主启动类
@SpringBootApplication
@EnableServiceComb
public class EdgeApplication {
public static void main ( String [ ] args) {
new SpringApplicationBuilder ( ) . web ( WebApplicationType . NONE) . sources ( EdgeApplication . class ) . run ( args) ;
}
}
(六)验证
分别启动chassis-provider、chassis-consumer 和 edge 访问 http://localhost:5000/sayHello?name=chassis,返回 “Hello chassis, This is Spring Cloud Eureka!” 则项目搭建成功。